//
//  XJStatusPictureView.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/25.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit
//重用标识符
private let cellId = "cell"
//间距
let cellMargin: CGFloat = 5
//cell的宽高
let cellWH: CGFloat = 80
class XJStatusPictureView: UICollectionView {
    
    var pictureInfo: [XJPictureInfo]?{
        
        didSet{
            
            //            countLabel.text = "\(pictureInfo?.count ?? 0)"
            //配图的size
            let size = getPicutreViewSize(pictureInfo?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            //刷新UI
            reloadData()
        }
    }
    //计算配图的size
    func getPicutreViewSize(count: Int) -> CGSize{
        //计算行数的列数
        let col = count == 4 ? 2 : count>=3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * cellMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * cellMargin
        
        return CGSize(width: w, height: h)
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置界面
    func setupUI() {
        
        backgroundColor = RandomColor()
        
        let layout = self.collectionViewLayout as!UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumInteritemSpacing = cellMargin
        
        layout.minimumLineSpacing = cellMargin
        
        registerClass(XJStatusPictureViewCell.self, forCellWithReuseIdentifier: cellId)
        
        dataSource = self
        
        addSubview(countLabel)
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)
    
}

extension XJStatusPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! XJStatusPictureViewCell
        cell.pic_url = pictureInfo![indexPath.item]
        return cell
        
    }
}

class XJStatusPictureViewCell: UICollectionViewCell {
    
    var pic_url: XJPictureInfo?{
        didSet{
            bgImageView.xj_setImage(pic_url?.thumbnail_pic, placeHolderImgName: "weibo_placeholder")
            
            //处理动态图
            if let thPic = pic_url?.thumbnail_pic where thPic.hasSuffix(".gif"){
                gifImageView.hidden = false
            }else{
                gifImageView.hidden = true
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor.clearColor()
        
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
    }
    //懒加载控件
    //背景图片
    private lazy var bgImageView: UIImageView = {
        let imgView = UIImageView(imgName: "weibo_placeholder")
        imgView.contentMode = .ScaleAspectFill
        imgView.layer.masksToBounds = true
        return imgView
    }()
    //动态图片
    private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")
    
}
























